package com.sundablog.demo.action;

import com.sundablog.demo.service.IModifyService;
import com.sundablog.demo.service.IQueryService;
import com.sundablog.spring.framework.annotation.SDAutowired;
import com.sundablog.spring.framework.annotation.SDController;
import com.sundablog.spring.framework.annotation.SDRequestMapping;
import com.sundablog.spring.framework.annotation.SDRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Title:
 * @Package com.sundablog.demo.action
 * @Description: TODO
 * @author: 孙达
 * @date: 2020/04/16 14:56
 * @Copyright: All rights Reserved, Designed By www.sundablog.com
 */
@SDController
@SDRequestMapping("/web")
public class MyAction {
    @SDAutowired
    IQueryService queryService;
    @SDAutowired
    IModifyService modifyService;

    @SDRequestMapping("/query.json")
    public void query(HttpServletRequest request, HttpServletResponse response,
                      @SDRequestParam("name") String name){
        String result = queryService.query(name);
        out(response,result);
    }

    @SDRequestMapping("/add*.json")
    public void add(HttpServletRequest request,HttpServletResponse response,
                    @SDRequestParam("name") String name,@SDRequestParam("addr") String addr){
        String result = modifyService.add(name,addr);
        out(response,result);
    }

    @SDRequestMapping("/remove.json")
    public void remove(HttpServletRequest request,HttpServletResponse response,
                       @SDRequestParam("id") Integer id){
        String result = modifyService.remove(id);
        out(response,result);
    }

    @SDRequestMapping("/edit.json")
    public void edit(HttpServletRequest request,HttpServletResponse response,
                     @SDRequestParam("id") Integer id,
                     @SDRequestParam("name") String name){
        String result = modifyService.edit(id,name);
        out(response,result);
    }



    private void out(HttpServletResponse resp,String str){
        try {
            resp.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
